package com.itcanteen.medical.patient.service.impl;

import com.itcanteen.medical.patient.entity.Information;
import com.itcanteen.medical.patient.mapper.InformationMapper;
import com.itcanteen.medical.patient.service.InformationService;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * 新闻 业务层
 * @author 1804A-TeamOne
 * @date 2020-12-17 19:11
 */
@Service
public class InformationServiceImpl implements InformationService {

    @Resource
    InformationMapper informationMapper;
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 查询全部
     * @return java.util.List<com.itcanteen.medical.patient.entity.Informarion>
     */
    @Override
    public List<Information> selectAll() {
        return informationMapper.selectAll();
    }

    /**
     * 根据id查询新闻
     * @param id 新闻id
     * @return com.itcanteen.medical.patient.entity.Informarion
     */
    @Override
    public Information selectOne(Integer id) {
        return informationMapper.selectByPrimaryKey(id);
    }

    /**
     * 排序列表 热点资讯
     * @return java.util.List<com.itcanteen.medical.patient.entity.Informarion>
     */
    @Override
    public List<Information> orderBy() {
        return informationMapper.orderBy();
    }

    /**
     * 定时更新数据库新闻点击量
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void weight(){
        List<Information> informationList = informationMapper.selectAll();
        for (Information information : informationList) {
            //把当前Id的redis 次数取出来  修改 当前新闻的权重
            Integer sum = (Integer)redisTemplate.boundHashOps("information").get("information" + information.getId());
            redisTemplate.boundHashOps("information").delete("information"+ information.getId());
            sum=sum==null?0:sum;
            information.setClicker(information.getClicker()+sum);
            information.setUpdateTime(new Date());
            informationMapper.updateByPrimaryKey(information);
        }
    }
}
